/*

	Name:      CoolFade
	Purpose:	 Fading all DOM object
	Author:    Lorenzo Macchiavelli - lmacchiavelli@gmail.com
	Created:   28/11/2011
	License:	 (MIT - X11 license) - read license.txt or link: http://www.opensource.org/licenses/mit-license.php
	Copyright: (C) 2011 Lorenzo Macchiavelli, 
  Project:   http://code.google.com/p/coolfade/

*/

(function($) {

	var methods = {
		init : function(options) {

			var settings = $.extend( {
					
					'time':'1000',
					'mouseOver_SP':'yes',
					'height':'200',
					'width':'200',
					'resize':'no'
					
			}, options);
			
			var $set = settings;
			var $this = $(this);
			var dim = $this.children().length-1;
			
			$this.addClass('cfade');
			
			$('.cfade').each(function(i){
				
					$(this).data('id',{id:i})
					$(this).attr('id','cfade_'+i)
					 
			})
			
			var index = $(this).data('id').id;//this.index();
			
			if($set.resize != "no"){
						 
       $this.css(
          {
                'overflow':'hidden',
                'display':'block',
                'position':'relative',
                'height':$set.height+'px',
                'width':$set.width+'px'
          }
       )	
    
    }else{
        
       $this.css(
          {
                'display':'block',
                'position':'relative',
          }
       )
    }
			
			$('*',this).css(
				{
					'border':'none',
					'margin':'0',
					'padding':'0',
					'list-style-type':'none',
					'position':'absolute'
				}
			)
						
			sorting(dim+5,index);
			
		  if( $this.children().size() > 1 ){
				time($set.time,dim,index,1);
			}else{
				time('','','',0);
			}
						
			function time(timevalue,element,index,stato){
				
				jQuery.globalEval((stato) ? $this.interval = setInterval("cicling("+element+","+index+","+timevalue+")", timevalue) : clearInterval($this.interval));
				
			}
			
			
			if($set.mouseOver_SP=="yes"){
				
				$this.mouseover(function(){
					time('','','',0);
				
				})
				$this.mouseout(function(){
					
					sorting(dim+5,index);
					time($set.time,dim,index,1);
				
				})

			}
         
      
      function startAutopager() {
      if($this.interval){
      clearInterval($this.interval);
      }
      
      time($set.time,dim,index,1);
      
      
      }
      function stopAutopager() {
      clearInterval($this.interval);
      }
      
        window.addEventListener('focus', startAutopager);    
        window.addEventListener('blur', stopAutopager);
         
         
		}
	};

	$.fn.coolfade = function(method) {
		
		if (methods[method]) {
			
		return methods[method].apply(this, Array.prototype.slice.call(
		
			arguments, 1));
		
		} else if (typeof method === 'object' || !method) {
		
			return methods.init.apply(this, arguments);
		
		} else {
			
			$.error('Method ' + method + ' does not exist on jQuery.coolfade');
		
		}
	};

})(jQuery);

function sorting(element,index){
		
	ind = element;
	
	$('#cfade_'+index).children().each(function(i){
		
		--ind;
		$(this).css('z-index',ind);
	
	})
}



var count=0;
function cicling(element,index,timevalue){
	
	$('#cfade_'+index).children(':first-child').animate({
	
		opacity: 0
	
	},'slow',function() {
    
	$('#cfade_'+index).children(':first-child').appendTo('#cfade_'+index).fadeTo(0,1);
	
		sorting(element+5,index);
  	
	});
	
	( count <= element ) ? ++count : count = 0;

}